package org.lql.algo.codecrush.hot100.dynamicProgrammingMulti;

/**
 * @author: liangqinglong
 * @date: 2025-09-15 22:54
 * @description: 1143. 最长公共子序列 <a href="https://leetcode.cn/problems/longest-common-subsequence/description/?envType=study-plan-v2&envId=top-100-liked">...</a>
 **/
public class LongestCommonSubsequence {

	public int longestCommonSubsequence(String text1, String text2) {
		int m = text1.length();
		int n = text2.length();
		text1 = " " + text1;
		text2 = " " + text2;
		int[][] dp = new int[m + 1][n + 1];
		for (int i = 1; i <= m; i++) {
			for (int j = 1; j <= n; j++) {
				if (text1.charAt(i) == text2.charAt(j)) {
					dp[i][j] = dp[i - 1][j - 1] + 1;
				} else {
					// 当两个字符不相等时，取最大值
					dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
				}
			}
		}
		return dp[m][n];
	}
}
